package cn.iocoder.yudao.module.member.controller.app.user;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.annotations.PreAuthenticated;
import cn.iocoder.yudao.module.member.controller.app.user.vo.*;
import cn.iocoder.yudao.module.member.convert.user.MemberUserConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.level.MemberLevelDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import cn.iocoder.yudao.module.promotion.api.yearcardorder.YearCardOrderApi;
import cn.iocoder.yudao.module.promotion.api.yearcardorder.dto.YearCardOrderRespDTO;
import cn.iocoder.yudao.module.system.api.partner.PartnerApi;
import cn.iocoder.yudao.module.system.api.partner.dto.PartnerRespDTO;
import cn.iocoder.yudao.module.trade.api.storeusers.StoreUsersApi;
import cn.iocoder.yudao.module.trade.api.storeusers.dto.StoreUsersRespDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 用户个人中心")
@RestController
@RequestMapping("/member/user")
@Validated
@Slf4j
public class AppMemberUserController {

    @Resource
    private MemberUserService userService;
    @Resource
    private MemberLevelService levelService;
    @Resource
    private YearCardOrderApi yearCardOrderApi;
    @Resource
    private PartnerApi partnerApi;
    @Resource
    private StoreUsersApi storeUsersApi;
    @Resource
    private MemberPointRecordService memberPointRecordService;

    @GetMapping("/get")
    @Operation(summary = "获得基本信息")
    @PreAuthenticated
    public CommonResult<AppMemberUserInfoRespVO> getUserInfo() {
        MemberUserDO user = userService.getUser(getLoginUserId());
        MemberLevelDO level = levelService.getLevel(user.getLevelId());
        AppMemberUserInfoRespVO result = MemberUserConvert.INSTANCE.convert(user, level);
        YearCardOrderRespDTO yearCardOrderRespDTO = yearCardOrderApi.getEnabledYearCardOrderByMemberId(user.getId());
        if (yearCardOrderRespDTO != null) {
            result.setYearCardEnabled(Boolean.TRUE);
        }
        PartnerRespDTO partnerRespDTO = partnerApi.getEnabledPartnerByPhone(user.getMobile());
        if (partnerRespDTO != null) {
            result.setPartnerEnabled(Boolean.TRUE);
        }
        List<StoreUsersRespDTO> storeUsers = storeUsersApi.listEnabledStoreUsersByMobile(user.getMobile());
        if (CollUtil.isNotEmpty(storeUsers)) {
            result.setStoreUserEnabled(Boolean.TRUE);
            result.setStoreUserRole(storeUsers.get(0).getRole());
        }
        int totalPoint = memberPointRecordService.getTotalPoint(user.getId());
        result.setPoint(totalPoint);
        return success(result);
    }

    @PutMapping("/update")
    @Operation(summary = "修改基本信息")
    @PreAuthenticated
    public CommonResult<Boolean> updateUser(@RequestBody @Valid AppMemberUserUpdateReqVO reqVO) {
        userService.updateUser(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-mobile")
    @Operation(summary = "修改用户手机")
    @PreAuthenticated
    public CommonResult<Boolean> updateUserMobile(@RequestBody @Valid AppMemberUserUpdateMobileReqVO reqVO) {
        userService.updateUserMobile(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/update-password")
    @Operation(summary = "修改用户密码", description = "用户修改密码时使用")
    @PreAuthenticated
    public CommonResult<Boolean> updatePassword(@RequestBody @Valid AppMemberUserUpdatePasswordReqVO reqVO) {
        userService.updateUserPassword(getLoginUserId(), reqVO);
        return success(true);
    }

    @PutMapping("/reset-password")
    @Operation(summary = "重置密码", description = "用户忘记密码时使用")
    public CommonResult<Boolean> resetPassword(@RequestBody @Valid AppMemberUserResetPasswordReqVO reqVO) {
        userService.resetUserPassword(reqVO);
        return success(true);
    }

}

